<div class="content-wrapper">
	<section class="content-header">
		<h1>{$userInfo?'编辑':'新增'}用户</h1>
		<ol class="breadcrumb">
			<li><a href="/admin"><i class="fa fa-dashboard"></i> 首页</a></li>
			<li class="active">账号管理</li>
			<li class="active"><a href="/admin/user/userList">用户列表</a></li>
			<li class="active">{$userInfo?'编辑':'新增'}用户</li>
		</ol>
	</section>
	<section class="content">
		<div class="box">
			<div class="box-header with-border">				
			</div>
			<div class="box-body">
				<form class="form-horizontal" role="form" action="javascrip:;" method="post">
					<input type="hidden" name="user_id" id="user_id" value="{$userInfo.id}" />
					<div class="form-group">
						<label for="username" class="col-sm-3 control-label"> 用户名 </label>
						<div class="col-sm-6">
							<input type="text" class="form-control" placeholder="用户名" id="username" name="username" value="{$userInfo.username}">
						</div>
					</div>

					<div class="form-group">
						<label for="password" class="col-sm-3 control-label"> 密码 </label>
						<div class="col-sm-6">
							<input type="password" class="form-control" placeholder="请输入6-16位数字或字母密码" id="password" name="password" value="{$userInfo.password}">
						</div>
					</div>

					<div class="form-group">
						<label for="name" class="col-sm-3 control-label"> 姓名 </label>
						<div class="col-sm-6">
							<input type="text" class="form-control" placeholder="姓名" id="name" name="name" value="{$userInfo.name}">
						</div>
					</div>
					
					<div class="form-group">
						<label for="nickname" class="col-sm-3 control-label"> 昵称 </label>
						<div class="col-sm-6">
							<input type="text" class="form-control" placeholder="昵称" id="nickname" name="nickname" value="{$userInfo.nickname}">
						</div>
					</div>

					<div class="form-group">
						<label for="sex" class="col-sm-3 control-label"> 性别 </label>
						<div class="col-sm-6">
							<select name="sex" id="sex" class="form-control">
								<option value="1" <if condition="$userInfo.sex eq 1">selected</if>>男</option>
								<option value="2" <if condition="$userInfo.sex eq 2">selected</if>>女</option>
							</select>
						</div>
					</div>

					<div class="form-group">
						<label for="department_id" class="col-sm-3 control-label"> 部门 </label>
						<div class="col-sm-6">
							<select name="department_id" id="department_id" class="form-control">
								<foreach name="departmentList" item="department">
									<option value="{$department.id}" <if condition="$department['id'] eq $userInfo['department_id']">selected</if>>{$department.name}</option>
								</foreach>
							</select>
						</div>
					</div>

					<!--<div class="form-group">
						<label for="position_id" class="col-sm-3 control-label"> 岗位 </label>
						<div class="col-sm-6">
							<select name="position_id" id="position_id" class="form-control">
								<foreach name="positionList" item="position">
									<option value="{$position.id}"<if condition="$position['id'] eq $userInfo['position_id']">selected</if>>{$position.name}</option>
								</foreach>
							</select>
						</div>
					</div>-->

					<div class="form-group">
						<label for="work_number" class="col-sm-3 control-label"> 工号 </label>
						<div class="col-sm-6">
							<input type="text" class="form-control" name="work_number" id="work_number" value="{$userInfo.work_number}" />
						</div>
					</div>
					
					<div class="form-group">
						<label for="wechat" class="col-sm-3 control-label"> 微信号 </label>
						<div class="col-sm-6">
							<input type="text" class="form-control" name="wechat" id="wechat" value="{$userInfo.wechat}" />
						</div>
					</div>
					
					<div class="form-group">
						<label for="qq" class="col-sm-3 control-label"> QQ </label>
						<div class="col-sm-6">
							<input type="text" class="form-control" name="qq" id="qq" value="{$userInfo.qq}" />
						</div>
					</div>
					
					<div class="form-group">
						<label for="email" class="col-sm-3 control-label"> 邮箱 </label>
						<div class="col-sm-6">
							<input type="text" class="form-control" name="email" id="email" value="{$userInfo.email}" />
						</div>
					</div>
					
					<div class="form-group">
						<label for="school" class="col-sm-3 control-label"> 毕业学校 </label>
						<div class="col-sm-6">
							<input type="text" class="form-control" placeholder="毕业学校-学院（系）-专业" name="school" id="school" value="{$userInfo.school}" />
						</div>
					</div>
					
					<div class="form-group">
						<label for="address" class="col-sm-3 control-label"> 地址 </label>
						<div class="col-sm-6">
							<input type="text" class="form-control" name="address" id="address" value="{$userInfo.address}" />
						</div>
					</div>
					
					<div class="form-group">
						<label for="skill" class="col-sm-3 control-label"> 技能 </label>
						<div class="col-sm-6">
							<input type="text" class="form-control" placeholder="多个技能用逗号（,）分隔" name="skill" id="skill" value="{$userInfo.skill}" />
						</div>
					</div>

					<div class="form-group">
						<label for="file_upload_btn" class="col-sm-3 control-label"> 照片 </label>
						<div class="col-sm-6">
							<input type="file" style="width: 0; height: 0; overflow: hidden;" name="upload_file" id="upload_file" /> 
							<input type="hidden"id="hidden_img" name="img" value="{$userInfo.img}" />
							<button type="button" id="file_upload_btn" class="btn btn-purple btn-sm" onclick="$('#upload_file').trigger('click')">上传图片</button>
							<empty name="userInfo.img"> 
								<br><br><img id="file_upload_show" width="100" height="100" src="__PUBLIC__/AdminLTE/dist/img/user2-160x160.jpg" style="display: none;" />
							<else /> 
								<br><br><img id="file_upload_show" width="100" height="100" src="__PUBLIC__/{$userInfo['img']}" />
							</empty>
						</div>
					</div>

					<div class="form-group">
						<label for="desc" class="col-sm-3 control-label"> 员工介绍 </label>
						<div class="col-sm-6">
							<textarea class="form-control" name="desc" id="desc" class="col-xs-12">{$userInfo.desc}</textarea>
						</div>
					</div>

					<div class="form-group">
						<label for="form-field-1" class="col-sm-3 control-label"></label>
						<div class="col-sm-6">
							<button type="button" class="btn btn-info btn-sm save-btn">提交</button>
							&nbsp;
							<button type="reset" class="btn btn-sm">重置</button>
						</div>
					</div>
				</form>
			</div>			
		</div>
	</section>
</div>
<script type="application/javascript" src="__PUBLIC__/global/AjaxUpload.js"></script>
<script type="text/javascript">
	var hostUrl = window.location.host;
$(function(){
	//上传照片
	$("#upload_file").change(function(){
		window.uploadImage();
	});
	
	window.uploadImage = function() {
		var preview, img_txt, localImag, 
		file_head = document.getElementById("upload_file"), picture = file_head.value;

		if (!picture.match(/.jpg|.gif|.png|.bmp/i)) {
			//验证图片格式
			ArtDialogHlp.showInfo('提示', '您上传的图片格式不正确，请重新选择！');
			return false;
		}
		
		$.ajaxFileUpload({
			url : "http://"+hostUrl+"/admin/upload", //用于文件上传的服务器端请求地址
			secureuri : false, //是否需要安全协议，一般设置为false
			fileElementId : 'upload_file', //文件上传域的ID
			dataType : 'json', //返回值类型 一般设置为json
			data : {
				action : 'images',
				extend : 'authuserhead'
			},
			success : function(data, status) {
				if(data.Code == 999) {
                    $("#file_upload_show").show().attr("src",data.Data.file_url);
                    $("#hidden_img").val(data.Data.rel_url);
                } else {
                	ArtDialogHlp.showInfo('提示', data.Msg);
                }
				$("#upload_file").change(function(){
					window.uploadImage();
				});
			}
		});
	};
	
	//保存用户
	$(".save-btn").click(function(){
		var id = $("#user_id").val();
		var username = $.trim($("#username").val());
		var password = $.trim($("#password").val());
		var name = $.trim($("#name").val());
		var nickname = $.trim($("#nickname").val());
		var sex = $("#sex").val();
		var department_id = $("#department_id").val();
		// var position_id = $("#position_id").val();
		var work_number = $.trim($("#work_number").val());
		var wechat = $.trim($("#wechat").val());
		var qq = $.trim($("#qq").val());
		var email = $.trim($("#email").val());
		var school = $.trim($("#school").val());
		var address = $.trim($("#address").val());
		var skill = $.trim($("#skill").val());
		var img = $("#hidden_img").val();
		var desc = $.trim($("#desc").val());
		
		if(username == '') {
			ArtDialogHlp.showInfo('提示', '请填写用户名');
            return false;
        }
		
		if(password == '') {
			ArtDialogHlp.showInfo('提示', '请填写密码');
            return false;
        }
		
		if(name == '') {
			ArtDialogHlp.showInfo('提示', '请填写用户姓名');
            return false;
        }
		
		$.post('/admin/user/saveUser', 
				{
					id : id,
					username : username,
					password : password,
					name : name,
					nickname : nickname,
					sex : sex,
					department_id : department_id,
					//position_id : position_id,
					work_number : work_number,
					wechat : wechat,
					qq : qq,
					email : email,
					school : school,
					address : address,
					skill : skill,
					img : img,
					desc : desc
				}, function(data){
			if(data.Code == 999) {
				ArtDialogHlp.showInfo('提示', '操作成功', function(){
					window.location.href = '/admin/user/userList';
				});
			} else {
				ArtDialogHlp.showInfo('提示', data.Msg);
			}
		}, 'json');
	});
});
</script>